package com.epalmpay.service.trade.impl;

import com.epalmpay.dto.trade.MemberUploadRecordInDTO;
import com.epalmpay.entity.Page;
import com.epalmpay.entity.TradeUpload;
import com.epalmpay.mapper.TradeUploadMapper;
import com.epalmpay.service.trade.IMemberUploadRecordService;
import com.epalmpay.util.DateUtil;
import com.epalmpay.util.Tools;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wucongpeng on 2017/5/7.
 */
@Service("devUploadRecordService")
public class MemberUploadRecordServiceImpl implements IMemberUploadRecordService{

    @Resource
    private TradeUploadMapper tradeUploadMapper;



    public List<TradeUpload> listMemberUploadRecords(MemberUploadRecordInDTO memberUploadRecordInDTO, Page page) throws Exception {


        TradeUpload tradeUpload = new TradeUpload();
        tradeUpload.setPage(page);

        tradeUpload.setAgent(memberUploadRecordInDTO.getAgentId());
        tradeUpload.setShopId(memberUploadRecordInDTO.getShopId());
        tradeUpload.setGroupId(memberUploadRecordInDTO.getGroupId());
        tradeUpload.setMerchantName(memberUploadRecordInDTO.getShopName());
        tradeUpload.setMemberName(memberUploadRecordInDTO.getCardName());
        tradeUpload.setAuditStatus(memberUploadRecordInDTO.getAuditStatus());
        if (!Tools.isEmpty(memberUploadRecordInDTO.getJoinStart())) {
            tradeUpload.setJoinStart(DateUtil.fomatDate(memberUploadRecordInDTO.getJoinStart()));
        }
        if (!Tools.isEmpty(memberUploadRecordInDTO.getJoinEnd())) {
            tradeUpload.setJoinEnd(DateUtil.getDateNext(memberUploadRecordInDTO.getJoinEnd(), 1));
        }

        return tradeUploadMapper.memberRecordUploadlistPage(tradeUpload);
    }


    /**
     * 根据当前选中项,更新状态,然后标记更新成功标志或者失败标志,存放列表
     * @param memberUploadRecordInDTOList
     * @return
     */
    public List<Integer> updateAuditStatus(List<MemberUploadRecordInDTO> memberUploadRecordInDTOList) {

        List<Integer> list = new ArrayList<Integer>();


        for (int i = 0; i<memberUploadRecordInDTOList.size(); i++){
            TradeUpload tradeUpload = new TradeUpload();
            tradeUpload.setGroupId(memberUploadRecordInDTOList.get(i).getGroupId());
            tradeUpload.setId(memberUploadRecordInDTOList.get(i).getId());
            tradeUpload.setAuditStatus(memberUploadRecordInDTOList.get(i).getAuditStatus());
            list.add(tradeUploadMapper.updateByPrimaryKeySelective(tradeUpload));
        }

        return list;
    }




}
